package com.party.web.biz.serviceOrder;

import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.core.model.charge.PackageMember;
import com.party.core.model.charge.ProductPackage;
import com.party.core.model.charge.ProductType;
import com.party.core.model.member.MemberGroupInfo;
import com.party.core.model.partner.MemberPartner;
import com.party.core.service.charge.IPackageMemberService;
import com.party.core.service.charge.IPackageService;
import com.party.core.service.member.IMemberGroupInfoService;
import com.party.core.service.member.impl.MemberGroupInfoService;
import com.party.core.service.partner.IMemberPartnerService;
import com.party.web.biz.charge.PackageBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: “服务订购”业务服务
 * @author: Hua Qunhao
 * @create: 2019-05-24 12:40
 **/
@Service
public class ServiceOrderBizService {

    @Autowired
    private IPackageMemberService packageMemberService;

    @Autowired
    private IMemberPartnerService memberPartnerService;

    @Autowired
    private IMemberGroupInfoService memberGroupInfoService;

    @Autowired
    private IPackageService packageService;

    @Autowired
    private PackageBizService packageBizService;
    /**
     * @Description: 是否存在待支付的订单
     * @Param:
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019-05-30-0030
     */
    public boolean isUnpaidOrder(String memberId) {
        return packageMemberService.isUnpaidOrder(memberId);
    }

    /**
     * @Description: 未到期的情况下是否存在降级
     * @Param: memberId productId
     * @return: boolean
     * @Author: Hua Qunhao
     * @Date: 2019-05-30-0030
     */
    public boolean existsDemotion(String memberId, String productId) {
        MemberPartner dbmemberPartner = memberPartnerService.findByMemberId(memberId);
        MemberGroupInfo dbmemberGroupInfo = memberGroupInfoService.findByMemberId(memberId);
        ProductPackage productPackage = packageService.get(productId);

        //未到期 &&  以前购买的是众筹 &&   现在买的是社群
        if (0 != dbmemberPartner.getSystemType()
                && dbmemberGroupInfo.getExpirationTime() != null
                && DateUtils.compareDate(dbmemberGroupInfo.getExpirationTime(), new Date()) == 1
                && ProductPackage.SYSTEM_TYPE_ZC == dbmemberPartner.getSystemType()
                && ProductPackage.SYSTEM_TYPE_SQ == productPackage.getSystemType()
        ) {
            //存在降级行为
            return true;
        }
        return false;
    }

    /**
    * @Author hzq
    * @Description //套餐升级、续费、购买  计算时间和金额
    * @Date 11:38 2019/5/31
    * @param productId
    * @param memberId
    * @return
    **/
    public PackageMember getPackageMember(String productId,String memberId){
        Map<String, Date> stringDateMap = new HashMap<>();
        ProductPackage productPackage = packageService.get(productId);
        List<PackageMember> packageMemberList = packageMemberService.getLatelyPaySuccess(memberId);
        if(packageMemberList.size()<1){
            return null;
        }
        PackageMember packageMember = packageMemberList.get(0);
        MemberGroupInfo byMemberId = memberGroupInfoService.findByMemberId(memberId);
        //到期时间和当前时间相差多少天
        int diffDate = DateUtils.diffDate(byMemberId.getExpirationTime(), new Date());
        if(null == packageMember){
            return packageMember;
        }else if(packageMember.getSystemType() == productPackage.getSystemType()){
            //套餐续费
            packageMember.setEndTime(byMemberId.getExpirationTime());
            stringDateMap = packageBizService.calculateEndDate(productPackage, packageMember);
            packageMember.setEndTime(stringDateMap.get("endTime"));
            packageMember.setStartTime(stringDateMap.get("startTime"));
            packageMember.setPrice(productPackage.getPrice());
            packageMember.setGoodsPrice((float) 0);
            packageMember.setDeductionAmount((float) 0);
            packageMember.setOriginalEndTime(DateUtils.formatDateTime(byMemberId.getExpirationTime()));//原到期时间
            packageMember.setOriginalResidueDay(diffDate);//原剩余多少天
        }else if(packageMember.getSystemType() != productPackage.getSystemType()){
              //套餐升级

            //获取社群一年的单价
            Integer sign = 1;
            productPackage.setSystemType(1);
            productPackage.setType(ProductType.SYSTEM_TYPE.getCode());
            List<ProductPackage> goodslist = packageService.getGoodslist(productPackage, sign);
            ProductPackage productPackage1 = goodslist.get(0);
            double div = BigDecimalUtils.div(productPackage1.getPrice(), 365);
            double deduction = BigDecimalUtils.round(BigDecimalUtils.mul(div, diffDate), 2);//抵扣价
            //获取众筹一年的单价
            productPackage.setSystemType(2);
            List<ProductPackage> goodslist1 = packageService.getGoodslist(productPackage, sign);
            ProductPackage productPackage2 = goodslist1.get(0);
            double div1 = BigDecimalUtils.div(productPackage2.getPrice(), 365);
            double round = BigDecimalUtils.round(BigDecimalUtils.mul(div1, diffDate), 2);
            double actualPaymentMoney = BigDecimalUtils.round(BigDecimalUtils.sub(round, deduction), 2);

            packageMember.setPrice((float) actualPaymentMoney);//实际应付金额
            packageMember.setGoodsPrice((float) round);//商品金额
            packageMember.setDeductionAmount((float) deduction);//抵扣金额
            packageMember.setStartTime(DateUtils.getTodayDate());
            packageMember.setEndTime(DateUtils.addDay(DateUtils.getTodayDate(),diffDate));
            packageMember.setOriginalEndTime(DateUtils.formatDateTime(byMemberId.getExpirationTime()));//原到期时间
            packageMember.setOriginalResidueDay(diffDate);//原剩余多少天
        }
        return packageMember;
    }
}
